package com.dongjiantong.web.protal.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.sys.model.SysLabelModel;
import com.dongjiantong.user.enums.OrgTypeEnum;
import com.dongjiantong.user.model.Organization;
import com.dongjiantong.user.model.OrganizationUser;
import com.dongjiantong.user.model.RegionAccount;
import com.dongjiantong.user.model.UserArea;
import com.dongjiantong.user.service.OrgUserAreaService;
import com.dongjiantong.user.service.OrganizationService;
import com.dongjiantong.user.service.RegionAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.*;

/**
 * Created by ThinkPad on 2017/12/29.
 */
@Slf4j
@OpenBean
public class OrgController {
    @Reference(version = "1.0.0")
    private OrganizationService organizationService;
    @Reference(version = "1.0.0")
    private RegionAccountService regionAccountService;
    @Reference(version = "1.0.0")
    private OrgUserAreaService orgUserAreaService;

    /**
     * 根据区域查询机构单位
     *
     * @param provinceId
     * @param cityId
     * @param regionId
     * @param streetId
     * @return
     */
    @OpenMethod(key = "web.Org.selectOrgByArea",
            paramNames = {"provinceId", "cityId", "regionId", "streetId"},
            httpMethods = RequestMethod.GET)
    public Response<List<Organization>> selectOrg(Integer provinceId, Integer cityId, Integer regionId, Integer streetId) {

        Organization organization = new Organization();
        organization.setOrgType(OrgTypeEnum.OrgType_organization.getCode());
        organization.setProvinceId(provinceId);
        organization.setCityId(cityId);
        organization.setRegionId(regionId);
        organization.setStreetId(streetId);
        //单位
        List<Organization> organizationList = organizationService.select(organization);
        List list = new ArrayList();
        if (organizationList.size() == 0) {
            return Response.ok(list);
        }
        //部门
        Organization organizationDep = new Organization();
        organizationDep.setOrgType(OrgTypeEnum.OrgType_department.getCode());
        for (Organization organization1 : organizationList) {
            Map map = new HashMap();
            map.put("value", organization1.getId());
            map.put("label", organization1.getName());
            organizationDep.setParentId(organization1.getId());
            List<Organization> organizationDepList = organizationService.select(organizationDep);
            List list1 = new ArrayList();
            for (Organization organization2 : organizationDepList) {
                Map map1 = new HashMap();
                map1.put("value", organization2.getId());
                map1.put("label", organization2.getName());
                list1.add(map1);
            }
            map.put("children", list1);
            list.add(map);
        }


        return Response.ok(list);
    }


    /**
     * 根据单位查询部门
     *
     * @param parentId
     * @return
     */
    @OpenMethod(key = "web.Org.selectOrg",
            paramNames = {"parentId"},
            httpMethods = RequestMethod.GET)
    public Response<List<Organization>> selectDep(Long parentId) {

        List<Organization> organizationList = organizationService.findDepByOrgId(parentId);
        return Response.ok(organizationList);
    }


    /**
     * 根据机构状态查询机构
     *
     * @param
     * @return
     */
    @OpenMethod(key = "web.Org.selectOrgByAreaAndType",
            paramNames = {"provinceId", "cityId", "regionId", "streetId", "orgType"},
            httpMethods = RequestMethod.GET)
    public Response<List<Organization>> selectOrgType(Integer provinceId, Integer cityId, Integer regionId, Integer streetId, Integer orgType) {
        Organization organization = new Organization();
        organization.setProvinceId(provinceId);
        organization.setCityId(cityId);
        organization.setRegionId(regionId);
        organization.setStreetId(streetId);
        organization.setOrgType(orgType);
        List<Organization> organizationList = organizationService.select(organization);
        return Response.ok(organizationList);
    }

    /**
     * 创建单位或部门
     *
     * @param organization
     * @param level
     * @return
     */
    @OpenMethod(key = "web.Org.createOrg",
            paramNames = {"organization", "level"},
            httpMethods = RequestMethod.POST)
    public Response<String> createOrg(Organization organization, Integer level) {

        try {
            RegionAccount regionAccount = new RegionAccount();
            regionAccount.setProvinceId(organization.getProvinceId());
            regionAccount.setCityId(organization.getCityId());
            regionAccount.setRegionId(organization.getRegionId());
            regionAccount.setStreetId(organization.getStreetId());
            regionAccount.setLevel(level);
            RegionAccount regionAccount1 = regionAccountService.selectOne(regionAccount);
            if (regionAccount1 == null) {
                throw new OPServerException("该地区还没有开通");
            }

            organization.setRegionAccountId(regionAccount1.getId());
            organization.setCreatedAt(new Date());
            organizationService.createOrganization(organization);
            return Response.ok("创建成功");
        } catch (OPServerException op) {
            throw op;
        } catch (Exception e) {
            log.error(e.getMessage());
            return Response.fail("创建失败");
        }

    }


    @OpenMethod(key = "web.area.selectUserArea",
            paramNames = {"userId"},
            httpMethods = RequestMethod.GET)

    public Response<List<UserArea>> areaList(Long userId) {
        List<UserArea> areaList = orgUserAreaService.queryByUserId(userId);
        return Response.ok(areaList);
    }


}
